home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / mach / hurd / mips / trampoline.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-22  |  9.3 KB  |  258 lines

  1. /* Set thread_state for sighandler, and sigcontext to recover.  MIPS version.
  2. Copyright (C) 1994 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4.  
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with the GNU C Library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include <hurd/signal.h>
  21. #include "thread_state.h"
  22.  
  23.  
  24. struct mach_msg_trap_args
  25.   {
  26.     /* This is the order of arguments to mach_msg_trap.  */
  27.     mach_msg_header_t *msg;
  28.     mach_msg_option_t option;
  29.     mach_msg_size_t send_size;
  30.     mach_msg_size_t rcv_size;
  31.     mach_port_t rcv_name;
  32.     mach_msg_timeout_t timeout;
  33.     mach_port_t notify;
  34.   };
  35.  
  36.  
  37. struct sigcontext *
  38. _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
  39.             int signo, int sigcode,
  40.             int rpc_wait,
  41.             struct machine_thread_all_state *state)
  42. {
  43.  
  44.   __label__ trampoline, rpc_wait_trampoline;
  45.   void *sigsp;
  46.   struct sigcontext *scp;
  47.  
  48.   if (ss->context)
  49.     {
  50.       /* We have a previous sigcontext that sigreturn was about
  51.      to restore when another signal arrived.  We will just base
  52.      our setup on that.  */
  53.       if (! setjmp (_hurd_sigthread_fault_env))
  54.     {
  55.       memcpy (&state->basic, &ss->context->sc_mips_thread_state,
  56.           sizeof (state->basic));
  57.       memcpy (&state->exc, &ss->context->sc_mips_exc_state,
  58.           sizeof (state->exc));
  59.       state->set = (1 << MIPS_THREAD_STATE) | (1 << MIPS_EXC_STATE);
  60.       if (state->exc.coproc_state & SC_COPROC_USE_FPU)
  61.         {
  62.           memcpy (&state->fpu, &ss->context->sc_mips_loat_state,
  63.               sizeof (state->fpu));
  64.           state->set |= (1 << MIPS_FLOAT_STATE);
  65.         }
  66.       assert (! rpc_wait);
  67.       /* The intr_port slot was cleared before sigreturn sent us the
  68.          sig_post that made us notice this pending signal, so
  69.          _hurd_internal_post_signal wouldn't do interrupt_operation.
  70.          After we return, our caller will set SCP->sc_intr_port (in the
  71.          new context) from SS->intr_port and clear SS->intr_port.  Now
  72.          that we are restoring this old context recorded by sigreturn,
  73.          we want to restore its intr_port too; so store it in
  74.          SS->intr_port now, so it will end up in SCP->sc_intr_port
  75.          later.  */
  76.       ss->intr_port = ss->context->sc_intr_port;
  77.     }
  78.       /* If the sigreturn context was bogus, just ignore it.  */
  79.       ss->context = NULL;
  80.     }
  81.   else if (! machine_get_basic_state (ss->thread, state))
  82.     return NULL;
  83.  
  84.   if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
  85.       !(ss->sigaltstack.ss_flags & (SA_DISABLE|SA_ONSTACK)))
  86.     {
  87.       sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
  88.       ss->sigaltstack.ss_flags |= SA_ONSTACK;
  89.       /* XXX need to set up base of new stack for
  90.      per-thread variables, cthreads.  */
  91.     }
  92.   else
  93.     sigsp = (char *) state->basic.r29;
  94.  
  95.   /* Set up the sigcontext structure on the stack.  This is all the stack
  96.      needs, since the args are passed in registers (below).  */
  97.   sigsp -= sizeof (*scp);
  98.   scp = sigsp;
  99.  
  100.   if (! setjmp (_hurd_sigthread_fault_env))
  101.     {
  102.       /* Set up the sigcontext from the current state of the thread.  */
  103.  
  104.       scp->sc_onstack = ss->sigaltstack.ss_flags & SA_ONSTACK ? 1 : 0;
  105.  
  106.       /* struct sigcontext is laid out so that starting at sc_gpr
  107.      mimics a struct mips_thread_state.  */
  108.       memcpy (&scp->sc_mips_thread_state,
  109.           &state->basic, sizeof (state->basic));
  110.  
  111.       /* struct sigcontext is laid out so that starting at sc_cause
  112.      mimics a struct mips_exc_state.  */
  113.       if (! machine_get_state (ss->thread, state, MIPS_EXC_STATE,
  114.                    &state->exc, &scp->sc_cause,
  115.                    sizeof (state->exc)))
  116.     return NULL;
  117.       if ((scp->sc_coproc_used & SC_COPROC_USE_FPU) &&
  118.       /* struct sigcontext is laid out so that starting at sc_fpr
  119.          mimics a struct mips_float_state.  This state
  120.          is only meaningful if the coprocessor was used.  */
  121.       ! machine_get_state (ss->thread, state, MIPS_FLOAT_STATE,
  122.                    &state->fpu,
  123.                    &scp->sc_mips_float_state, sizeof (state->fpu)))
  124.     return NULL;
  125.     }
  126.   else
  127.     /* We got a fault trying to write the stack frame.
  128.        We cannot set up the signal handler.
  129.        Returning NULL tells our caller, who will nuke us with a SIGILL.  */
  130.     return NULL;
  131.  
  132.   /* Modify the thread state to call the trampoline code on the new stack.  */
  133.   if (rpc_wait)
  134.     {
  135.       /* The signalee thread was blocked in a mach_msg_trap system call,
  136.      still waiting for a reply.  We will have it run the special
  137.      trampoline code which retries the message receive before running
  138.      the signal handler.
  139.      
  140.      To do this we change the OPTION argument in its registers to
  141.      enable only message reception, since the request message has
  142.      already been sent.  */
  143.  
  144.       /* The system call arguments are stored in consecutive registers
  145.      starting with a0 ($4).  */
  146.       struct mach_msg_trap_args *args = (void *) &state->basic.r4;
  147.  
  148.       assert (args->option & MACH_RCV_MSG);
  149.       /* Disable the message-send, since it has already completed.  The
  150.      calls we retry need only wait to receive the reply message.  */
  151.       args->option &= ~MACH_SEND_MSG;
  152.  
  153.       state->basic.pc = (int) &&rpc_wait_trampoline;
  154.       state->basic.r29 = (int) sigsp; /* $29 is the stack pointer register.  */
  155.       /* After doing the message receive, the trampoline code will need to
  156.      update the v0 ($2) value to be restored by sigreturn.  To simplify
  157.      the assembly code, we pass the address of its slot in SCP to the
  158.      trampoline code in v1 ($3).  */
  159.       state->basic.r3 = (int) &scp->sc_gpr[1];
  160.       /* We must preserve the mach_msg_trap args in a0..t2 ($4..$10).
  161.      Pass the handler args to the trampoline code in s1..s3 ($17..$19).  */
  162.       state->basic.r17 = signo;
  163.       state->basic.r18 = sigcode;
  164.       state->basic.r19 = (int) scp;
  165.     }
  166.   else
  167.     {
  168.       state->basic.pc = (int) &&trampoline;
  169.       state->basic.r29 = (int) sigsp;
  170.       state->basic.r4 = signo;
  171.       state->basic.r5 = sigcode;
  172.       state->basic.r6 = (int) scp;
  173.     }
  174.  
  175.   /* We pass the handler function to the trampoline code in at ($1).  */
  176.   state->basic.r1 = (int) handler;
  177.   /* In the callee-saved register s0 ($16), we save the SCP value to pass
  178.      to __sigreturn after the handler returns.  */
  179.   state->basic.r16 = (int) scp;
  180.  
  181.   return scp;
  182.  
  183.   /* The trampoline code follows.  This is not actually executed as part of
  184.      this function, it is just convenient to write it that way.  */
  185.  
  186.  rpc_wait_trampoline:
  187.   /* This is the entry point when we have an RPC reply message to receive
  188.      before running the handler.  The MACH_MSG_SEND bit has already been
  189.      cleared in the OPTION argument in our registers.  For our convenience,
  190.      $3 points to the sc_gpr[1] member of the sigcontext (saved v0 ($2)).  */
  191.   asm volatile
  192.     (".set noat; .set noreorder; .set nomacro\n"
  193.      /* Retry the interrupted mach_msg system call.  */
  194.      "li v0, -25\n"        /* mach_msg_trap */
  195.      "syscall\n"
  196.      /* When the sigcontext was saved, v0 was MACH_RCV_INTERRUPTED.  But
  197.     now the message receive has completed and the original caller of
  198.     the RPC (i.e. the code running when the signal arrived) needs to
  199.     see the final return value of the message receive in v0.  So
  200.     store the new v0 value into the sc_gpr[1] member of the sigcontext
  201.     (whose address is in v1 to make this code simpler).  */
  202.      "sw v0, (v1)\n"
  203.      /* Since the argument registers needed to have the mach_msg_trap
  204.     arguments, we've stored the arguments to the handler function
  205.     in registers s1..s3 ($17..$19).  */
  206.      "move a0, s1\n"
  207.      "move a1, s2\n"
  208.      "move a2, s3\n");
  209.  
  210.  trampoline:
  211.   /* Entry point for running the handler normally.  The arguments to the
  212.      handler function are already in the standard registers:
  213.  
  214.        a0    SIGNO
  215.        a1    SIGCODE
  216.        a2    SCP
  217.      */
  218.   asm volatile
  219.     ("jal $1; nop\n"        /* Call the handler function.  */
  220.      /* Call __sigreturn (SCP); this cannot return.  */
  221.      "j %0\n"
  222.      "move a0, s0"        /* Set up arg from saved SCP in delay slot.  */
  223.      : : "i" (&__sigreturn));
  224.  
  225.   /* NOTREACHED */
  226.   asm volatile (".set reorder; .set at; .set macro");
  227.  
  228.   return NULL;
  229. }
  230.  
  231. /* STATE describes a thread that had intr_port set (meaning it was inside
  232.    HURD_EINTR_RPC), after it has been thread_abort'd.  It it looks to have
  233.    just completed a mach_msg_trap system call that returned
  234.    MACH_RCV_INTERRUPTED, return nonzero and set *PORT to the receive right
  235.    being waited on.  */
  236. int
  237. _hurdsig_rcv_interrupted_p (struct machine_thread_all_state *state,
  238.                 mach_port_t *port)
  239. {
  240.   if (! setjmp (_hurd_sigthread_fault_env))
  241.     {
  242.       const unsigned int *pc = (void *) state->basic.pc;
  243.       if (state->basic.r2 == MACH_RCV_INTERRUPTED &&
  244.       pc[-1] == 0xc)    /* syscall */
  245.     {
  246.       /* We did just return from a mach_msg_trap system call
  247.          doing a message receive that was interrupted.
  248.          Examine the parameters to find the receive right.  */
  249.       struct mach_msg_trap_args *args = (void *) &state->basic.r4;
  250.  
  251.       *port = args->rcv_name;
  252.       return 1;
  253.     }
  254.     }
  255.  
  256.   return 0;
  257. }
  258.